<?php
/**
 * 支付
 * User: Small Y
 * Date: 2019/03/23.
 */

namespace app\api\controller;

use app\api\service\QrcodeServer;
use app\api\service\SxfApiService;
use think\Log;

class Jhpay extends Common
{
	/**
	 * 随行付聚合支付接口service.
	 *
	 * @var SxfApiService
	 */
	protected $sxfApiService;

	public function _initialize()
	{
		$this->sxfApiService = new SxfApiService();
		$this->returnData = [
			'code' => &$this->code,
			'message' => &$this->message,
			'data' => [],
		];
		parent::_initialize();
	}

	/**
	 * 随行付聚合支付接口
	 * 直接输出二维码 + 生成二维码图片文件
	 * User: wufeng
	 * Date: 2019/4/11.
	 *
	 * @throws \think\exception\DbException
	 */
	public function sxfpay()
	{
		$data = $this->request->param();
		$res = json_decode($this->sxfApiService->sxfPay($data), true);
		if ('SXF0000' === $res['code']) {
			$result = $res['respData']; //payUrl
			Log::record('返回参数='.json_encode($result), 'pay');

			$this->success('生成收款二维码成功', '', ['url' => $result['payUrl']]);
		} else {
			$this->error($res['msg'],'','',1);
		}
	}

	/**
	 * User: wufeng
	 * Date: 2019/4/11.
	 *
	 * @return mixed
	 */
//	public function payflow()
//	{
//		$url = input('img');
//		$paytype = input('paytype');
//		$logo_url = '';
//		if ('117' === $paytype) {
//			$logo_url = './education/images/wechat.jpg';
//		} elseif ('118' === $paytype) {
//			$logo_url = './education/images/alipay.jpg';
//		} elseif ('120' === $paytype) {
//			$logo_url = './education/images/unionpay.jpg';
//		}
//		// 自定义二维码配置
//		$config = [
//			'title' => true,
//			'title_content' => '校务助手收款码',
//			'logo' => true,
//			'logo_url' => $logo_url,
//			'logo_size' => 80,
//		];
//
//		// 直接输出
//		$qr_url = $url;
//		$qr_code = new QrcodeServer($config);
//		$qr_img = $qr_code->createServer($qr_url);
//		$imageString = base64_encode($qr_img);
//		$img = 'data:image/jpg;base64,'.$imageString;
//		//		echo "<img src='".$url."'>";
//
//		//		// 写入文件
//		//		$qr_url = $result['payUrl'];
//		//		$file_name = './static/qrcode';  // 定义保存目录
//		//
//		//		$config['file_name'] = $file_name;
//		//		$config['generate']  = 'writefile';
//		//
//		//		$qr_code = new QrcodeServer($config);
//		//		$rs = $qr_code->createServer($qr_url);
//		//		print_r($rs);
//		//		exit;
//		$this->assign('url', $img);
//
//		return view();
//	}
}
